package ru.spb.osll.myfit.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

public class StringUtils {

	public static <T> String join(Collection<T> collection, String sep) {
		String res = "";
		Iterator<T> iter = collection.iterator();

		while (iter.hasNext()) {
			res = res + iter.next().toString();
			if (iter.hasNext()) {
				res = res + sep;
			}
		}

		return res;
	}

	public static String splitCamelCase(String str) {
		String splitStr = new String(String.valueOf(str.charAt(0)));

		for (int i = 1; i < str.length(); ++i) {
			if (Character.isUpperCase(str.charAt(i)))
				splitStr += " ";
			splitStr += Character.toLowerCase(str.charAt(i));
		}
		return splitStr;
	}

	public static String removeChar(String s, char c) {
		String r = "";

		for (int i = 0; i < s.length(); ++i)
			if (s.charAt(i) != c)
				r += s.charAt(i);

		return r;
	}

	public static String streamToString(FileInputStream fis) throws IOException {
		StringOutputStream outputStream = new StringOutputStream();
		byte bytes[] = new byte[1024];
		int i;

		while ((i = fis.read(bytes)) != -1)
			outputStream.write(bytes, 0, i);

		outputStream.close();
		return outputStream.toString();
	}

	public static final Filter<String> FILTER_NOT_EMPTY = new Filter<String>() {
		public boolean passes(String arg) {
			return !arg.isEmpty();
		}
	};
}
